package com.yjc.drygoods.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.TypeReference;
import com.yjc.common.exception.BizCodeEnume;
import com.yjc.drygoods.ware.entity.InventoryAlertsEntity;
import com.yjc.drygoods.ware.exception.NotClearStockException;
import com.yjc.drygoods.ware.exception.PurchaseNumException;
import com.yjc.drygoods.ware.feign.ProductFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.drygoods.ware.entity.PurchaseDetailEntity;
import com.yjc.drygoods.ware.service.PurchaseDetailService;
import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-08-19 23:23:36
 */
@Slf4j
@RestController
@RequestMapping("ware/purchasedetail")
public class PurchaseDetailController {
    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    ProductFeignService productFeignService;

    /**
     * /ware/purchasedetail/list
     * 查询采购需求
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseDetailService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PurchaseDetailEntity purchaseDetail = purchaseDetailService.getById(id);
        R r = productFeignService.getTotalCatelogPathWithSpuId(purchaseDetail.getSpuId());
        if (r.getCode() != 0) {
            throw new RuntimeException("干货远程服务调用异常");
        }
        List<Integer> data = (List<Integer>) r.get("data");
        List<Long> collect = data.stream().map(item -> {
            return Long.parseLong(item.toString());
        }).collect(Collectors.toList());
        Long[] longs = collect.toArray(new Long[collect.size()]);
//        log.info("data:{}", longs[0].getClass());
        purchaseDetail.setCatelogPathWithSpu(longs);
        return R.ok().put("purchaseDetail", purchaseDetail);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseDetailEntity purchaseDetail) {
        purchaseDetail.setUpdateTime(new Date());
//        log.info("purchaseDetail:{}", purchaseDetail);
        purchaseDetailService.savePurchaseDetail(purchaseDetail);

        return R.ok();
    }

    /**
     * 加入采购需求（库存预警）
     */
    @PostMapping("/addPurchaseItem")
    public R addPurchaseItem(@RequestBody InventoryAlertsEntity inventoryAlertsEntity) {
        log.info("inventoryAlertsEntity:{}", inventoryAlertsEntity);
        purchaseDetailService.addPurchaseItem(inventoryAlertsEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseDetailEntity purchaseDetail) {
        purchaseDetail.setUpdateTime(new Date());
        if (purchaseDetail.getSpuNum() != null && (purchaseDetail.getSpuNum() > 300 || purchaseDetail.getSpuNum() < 0)) {
            throw new PurchaseNumException("采购数量：" + purchaseDetail.getSpuNum());
        }
        purchaseDetailService.updateById(purchaseDetail);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseDetailService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
